//
//  CALayer+TYZAdd.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/5/15.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "CALayer+TYZAdd.h"

@implementation CALayer (TYZAdd)

+ (CALayer *)drawLine:(UIView *)view frame:(CGRect)frame lineColor:(UIColor *)lineColor
{
    CALayer *line = [CALayer layer];
    line.frame = frame;
    line.backgroundColor = lineColor.CGColor;
    [view.layer addSublayer:line];
    return line;
}

/**
 *  绘制虚线到view
 *
 *  @param view        父视图
 *  @param frame       frame
 *  @param lineSpacing 虚线之间的间隔
 *  @param lineColor   颜色
 *
 *  @return
 */
+ (CAShapeLayer *)drawDashLine:(UIView *)view frame:(CGRect)frame lineSpacing:(CGFloat)lineSpacing lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = frame;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    // 设置虚线的颜色
    shapeLayer.strokeColor = lineColor.CGColor;
    
    // 设置虚线的宽度(高度)
    shapeLayer.lineWidth = frame.size.height;
    shapeLayer.lineJoin = kCALineJoinRound;
    // 设置线宽，线间距
    shapeLayer.lineDashPattern = @[[NSNumber numberWithFloat:lineWidth], [NSNumber numberWithFloat:lineSpacing]];
    // 设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, frame.origin.x+frame.size.width, 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    [view.layer addSublayer:shapeLayer];
    
    return shapeLayer;
}

/// 画视图的虚线
/// @param view 视图
/// @param frame 视图
/// @param lineSpacing lineSpacing description
/// @param lineColor lineColor description
/// @param lineWidth lineWidth description
+ (void)addBorderLayer:(UIView *)view frame:(CGRect)frame lineSpacing:(CGFloat)lineSpacing lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth
{
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.bounds = view.bounds;
    borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:view.layer.cornerRadius].CGPath;
    borderLayer.lineWidth = 2;
    borderLayer.lineDashPattern = @[[NSNumber numberWithFloat:lineWidth], [NSNumber numberWithFloat:lineSpacing]];//@[@8, @8];
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [view.layer addSublayer:borderLayer];
}

/**
 *  视图部分边缘圆角(在矩形中，可以针对四角中的某个角加圆角, 一般用于设置某个视图的顶端两角为圆形)
 *
 *  @param bounds 视图的bounds
 *  @param cornerRadii 圆角的大小
 *  @param roundingCorners 枚举值，可以选择某个角
 */
+ (CAShapeLayer *)bezierPathRound:(CGRect)bounds cornerRadii:(CGSize)cornerRadii roundingCorners:(UIRectCorner)roundingCorners
{
    CAShapeLayer *styleLayer =[CAShapeLayer layer];
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(roundingCorners) cornerRadii:cornerRadii];
    styleLayer.path = shadowPath.CGPath;
    //    _acserTagLabel.layer.mask = styleLayer;
    return styleLayer;
}

@end
